<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Paypal extends CI_Controller
{
/**
*paypal 退款
*
*
**/
 function __construct()
    {
        parent::__construct();
        $this->load->model('Function_m', 'fun');
        $this->load->model('Paypal_m','pay');
    }

   public function index()
    {
        $this->tpl->display("paypal/welcome.tpl");
    }

 public function paypalRefundTo(){
 //退款页面申请退款
 //查询订单中被标记被退款的订单。
 
    $page=intval($this->input->get('page'));
    $order=$this->input->get('order');
    $orderd=$this->input->get('orderd');
    if(strlen($order)==0) $order=0;
    if(strlen($orderd)==0) $orderd='desc';

$this->displaytpl('*','*','*','order_sn',$page,$order,$orderd,'ordertime','','');
 }


public function ajaxbutton(){
$op = $this->input->get_post('op');
if ($op=='search') {
$osn =trim($this->input->get('kw'));
$sid = intval($this->input->get('shopID'));
$pfrom = intval($this->input->get('platformID'));
$where = trim($this->input->get('where'));
$order = trim($this->input->get('order'));
$orderd = trim($this->input->get('orderd'));
$page = trim($this->input->get('page'));
$timetype=trim($this->input->get('timetype'));
$starttime=trim($this->input->get('starttime'));
$endtime=trim($this->input->get('endtime'));
if(strlen($page)==0) $page='1';
if(strlen($osn)==0) $osn='*';
if(strlen($where)==0) $where='order_sn';
$this->displaytpl($osn,$sid,$pfrom,$where,$page,$order,$orderd,$timetype,$starttime,$endtime);
}

      //搜索
      if ($op=='searchnotes') {
     $where=trim($this->input->get('where'));
     $value=trim($this->input->get('value'));
     $value=(empty($value)?'*':$value);
     $TYPE=trim($this->input->get('TYPE'));
     $page=trim($this->input->get('page'));
     $page=(empty($page)?'1':$page);
     $starttime=trim($this->input->get('starttime'));
     $endtime=trim($this->input->get('endtime'));
     // $STATUS=trim($this->input->get('STATUS'));
     $STATUS= '*';
     if (empty($starttime)) $starttime='*';
     if (empty($endtime)) $endtime='*';

    $this->displaytplnotes($where, $value,$TYPE,$starttime,$endtime,$STATUS,$page);

      }

if ($op=='returnpay') {

 $this->displaytpl('*','*','*','order_sn',1,1,'asc','ordertime','','');
}


// if ($op=='selok') {
//  $this->displaytpl('*','*','*',3);
// }

// if ($op=='selfailure') {
//  $this->displaytpl('*','*','*',4);
// }

if ($op=='pop') {
		//弹框显示内容
$osn =intval($this->input->post('pay_id'));

echo $this->pay->RefundToinfo($osn);

}

if ($op=='agree') {
	$osn=$this->input->post('pay_id');
    $osn1=$this->input->post('order_sn');
//批准退款
echo $this->pay->Agree($osn1,$osn);

}

if ($op=='cancel') {
		$osn=intval($this->input->post('pay_id'));
//取消退款
$this->pay->Cancel($osn);
}

//定时执行退款流程
if ($op=='paypalRefund') {
	        $this->load->library('PaypalRefund');
			$ress = $this->paypalrefund->paypalRefund();
}

if($op=='paypalcheck'){
            $list=$this->input->get('list');
            $this->load->library('PaypalRefund');
            $ress = $this->paypalrefund->paypalcheck($list);
            print_r($ress);
}

//返回magento
if($op=='paypalreturn'){
$this->load->library('Magentoreturnpaypal');
$this->magentoreturnpaypal->returne();

}

//回执
if ($op == 'receipt') {
    $pay_id=$this->input->post('pay_id');
    $payreturnid = $this->input->post('payreturnid');
    $this->pay->receipt($pay_id,$payreturnid);
}


}




public function displaytpl($osn,$sid,$pfrom,$where,$page,$order,$orderd,$timetype,$starttime,$endtime){
if($page==0) $page=1;
$sql=$this->pay->SelectApp($osn,$sid,$pfrom,$where,$order,$orderd,$timetype,$starttime,$endtime);

$selectarr=$this->pay->selectarr($sql,$page);

$selectnum=$this->pay->SelectNum($sql);
$d_shop=$this->pay->SelectShop();
$platform=$this->pay->SelectPlatform();
//$count=$this->pay->SelectNum();
//获取人民币汇率
$allordermon=0;
$refunmon=0;
foreach ($selectnum as $value) {
//计算总计退款订单金额
$allordermon+=$value['goods_amount'];
$refunmon+=$value['refund_money']*$this->pay->tormb($value['moneytype']);
//计算总计退款金额
}
$count=count($selectnum);
$nowpage=$page;
$lastpage=$nowpage-1;
if($lastpage<=0) $lastpage=1;
$nextpage=$nowpage+1;

$pages=ceil($count/20);
if($nextpage>=$pages) $nextpage=$pages;
$rand=mt_rand(0,10000);
$pg=array();
if ($nowpage+5<$pages) {
    for ($i=$nowpage; $i <$nowpage+5 ; $i++) { 
        $pg[]=$i;
    }
}else{
    for ($i=$pages; $i >$pages-5 ; $i--) { 
    	  if($i<=0) continue;
         $pg[]=$i;
    }
}
if($osn=='*') $osn='';
//获取classid
$classid=$_COOKIE['classid'];
$this->tpl->assign('classid',$classid);
$this->tpl->assign('timetype',$timetype);
$this->tpl->assign('starttime',$starttime);
$this->tpl->assign('endtime',$endtime);
$this->tpl->assign('allordermon',$allordermon);
$this->tpl->assign('refunmon',number_format($refunmon,2));
$this->tpl->assign("nowuserid",$_COOKIE["userid"]);
$this->tpl->assign('platformID',$pfrom);
$this->tpl->assign('shopID',$sid);
$this->tpl->assign("pg",$pg);
$this->tpl->assign("d_shop",$d_shop);
$this->tpl->assign("platform",$platform);
$this->tpl->assign("selectarr",$selectarr);
$this->tpl->assign("status",$status);
$this->tpl->assign("rand",$rand);
$this->tpl->assign("nowpage",$nowpage);
$this->tpl->assign("pages",$pages);
$this->tpl->assign("lastpage",$lastpage);
$this->tpl->assign("nextpage",$nextpage);
$this->tpl->assign("zero",0);
$this->tpl->assign("sum",$count);
$this->tpl->assign("where",$where);
$this->tpl->assign("kw",$osn);
$this->tpl->assign("order",$order);
$this->tpl->assign("orderd",$orderd);
$this->tpl->display("paypal/application.tpl");


}

//paypal notes
    public function notes(){
      $this->displaytplnotes('*','*','*','*','*','*',1);
    }
//显示内容
    public function displaytplnotes($where,$value,$TYPE,$starttime,$endtime,$STATUS,$page){
     //构建sql语句
    $sql=$this->pay->displaytpl($where,$value,$TYPE,$starttime,$endtime,$STATUS);
    //echo $sql;
    //把sql语句放入函数查询出所需数据
    $list=$this->pay->getindextpl($sql,$page);
    //总计数量
    $sum=$this->pay->sqlcount($sql);
    //一直总数除以单页显示数量，然后分页显示
     $pages=ceil($sum/100);
     //页码地址
     $pageurl="/paypal/ajaxbutton?op=searchnotes&where=$where&value=$value&TYPE=$TYPE&starttime=$starttime&endtime=$endtime&STATUS=$STATUS";
     $this->tpl->assign('pageurl',$pageurl);
     $this->tpl->assign('pages',$pages);
     $this->tpl->assign('zero',0);
     $lastpage = $page-1;
     $this->tpl->assign('lastpage',$pages);
     $nextpage = $page+1;
     $this->tpl->assign('nextpage',$pages);
     $pg=array();
     $from=($page-3>0?($page-3):1);
     $end=($page+3<$pages?($page+3):$pages);
     for ($i=$from; $i <=$end ; $i++) {
      $pg[]=$i;
     }
     $this->tpl->assign("pg",$pg);
     $this->tpl->assign("sum",$sum);
    $this->tpl->assign("list",$list);
    if($value=='*') $value='';
    if($starttime=='*') $starttime='';
    if($endtime=='*') $endtime='';
    $this->tpl->assign("STATUS",$STATUS);
    $this->tpl->assign("value",$value);
    $this->tpl->assign("where",$where);
    $this->tpl->assign("TYPE",$TYPE);
    $this->tpl->assign("starttime",$starttime);
    $this->tpl->assign("endtime",$endtime);
    $this->tpl->assign('rand',mt_rand(0,1000));

    
    $this->tpl->display("paypal/show.tpl");
    }


}

?>